﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Drawing;
using System.Windows.Forms;
using System.IO;
using SyncLib.Client;

namespace SyncApplication.Business
{
    public class AnimationGrid:IDisposable
    {
        Image[] ImageList;
        public static Image ImgError;
        public static Image ImgWarning;
        public static Image ImgSuccess;
        public static Image ImgPending;
        volatile bool isStop = false;
        //public ClientSyncDS.TaskSyncListRow CurrentTask;
        int _count;
        System.Timers.Timer objTimer;
        public AnimationGrid()
        {
            LoadImage();
            objTimer = new System.Timers.Timer();
            objTimer.Interval = 500;
            objTimer.Elapsed += new System.Timers.ElapsedEventHandler(objTimer_Elapsed);
        }
        private void LoadImage()
        {
            List<Image> lst = new List<Image>();
            int i = 0;
            string basePath = Application.StartupPath + "\\Animation\\Grid\\";
            while (true)
            {
                if (File.Exists(basePath + "Running" + i + ".png"))
                {
                    lst.Add(Image.FromFile(basePath + "Running" + i + ".png"));
                    i++;
                }
                else
                    break;
            }
            ImageList = lst.ToArray();

            ImgError = Image.FromFile(basePath + "Error.png");
            ImgPending = Image.FromFile(basePath + "Pending.png");
            ImgSuccess = Image.FromFile(basePath + "Success.png");
            ImgWarning = Image.FromFile(basePath + "Warning.png");
        }
        ~AnimationGrid()
        {
            objTimer.Elapsed -= objTimer_Elapsed;
            objTimer.Dispose();
            for (int i = 0; i < ImageList.Length; i++)
                ImageList[i].Dispose();
            ImageList = null;
        }
        void objTimer_Elapsed(object sender, System.Timers.ElapsedEventArgs e)
        {
            if (isStop) return;
            if (_count >= ImageList.Length) _count = 0;
            SetImage(ImageList[_count++]);
        }
        public void Start()
        {
            _count = 0;
            isStop = false;
            objTimer.Enabled = true;
        }
        public void Stop()
        {
            objTimer.Stop();
            objTimer.Enabled = false;
            isStop = true;
        }
        public void Error()
        {
            Stop();
            SetImage(ImgError);
        }
        public void Warning()
        {
            Stop();
            SetImage(ImgWarning);
        }
        public void Success()
        {
            Stop();
            SetImage(ImgSuccess);

        }
        public void Skip()
        {
            Error();
        }
        private void SetImage(Image img)
        {
            //SyncDialog.Current.GridMain.Invoke(new MethodInvoker(delegate
            //{
            //    ClientSyncContext.CurrentTask.Image = img;
            //}));
            SyncDialog.Current.SetImage(ClientSyncContext.TaskIndex, img);
        }

        #region IDisposable Members

        public void Dispose()
        {
            objTimer.Stop();
            objTimer.Dispose();
        }

        #endregion
    }
}
